package net.cathackers.devel.scmp.xmpp;

import org.dom4j.Document;
import org.dom4j.Element;
/*
  Presence Packet Stanze :
  1-
  	<presence from='ex@cat-hackers.net' to='ex2@cat-hackers.net'>
  	 	<status>I am not here</status>
  	 	<show> chat|dnd|away </show>
  	 	<priority> 24 </priority>
 	<presence>
  
  2-	<presence type='unavailable|subscribe|unsubscribe|subscribed|unsubscribed|' to='ex@cat-hackers.net' from='ex2@cat-hackers.net'/>
 */
public class PresencePacket extends Packet implements Cloneable {

    public PresencePacket(Document doc) {
	super(doc);
    }

    public PresencePacket(PresenceType type) {
	this();
	setPresenceType(type);
    }

    protected PresencePacket() {
	super();
	rootElement_ = codeBehind_ .addElement("presence");
    }

    public PresenceType getPresenceType() {
	String type = rootElement_.attributeValue("type");
	if (type != null) {
		return PresenceType.valueOf(type.toUpperCase());
	} else {
		return PresenceType.AVAILABLE;
	}
    }

    public void setPresenceType(PresenceType presence) {
	rootElement_.addAttribute("type", presence.toString());
    }

    public String getShow() {
	Element found = rootElement_.element("show");
	if (null == found) {
	    return null;
	}

	return found.getText();
    }

    public void setShow(String show) {
	Element element = rootElement_.element("show");
	if (element == null) {
	    element = rootElement_.addElement("show");
	    element.setText(show);
	} else {
	    element.setText(show);
	}
    }

    public Integer getPriority() {
	Element found = rootElement_.element("priority");
	if (null == found) {
	    return null;
	}

	return Integer.parseInt(found.getText());
    }

    public void setPriority(int priority) {
	Element element = rootElement_.element("priority");
	if (element == null) {
	    element = rootElement_.addElement("priority");
	    element.setText(String.valueOf(priority));
	} else {
	    element.setText(String.valueOf(priority));
	}

    }

     public String getStatus(){
	 Element found = rootElement_.element("status");
	 if (null == found) {
	     return null;
	 }
     return found.getText();
     }
     
     public void setStatus(String status){
	 Element found = rootElement_.element("status");
		if (null == found) {
		    found = rootElement_.addElement("status");
		    found.setText(status);
		} else {
		    found.setText(status);
		}
     }
     
     public PresencePacket clone()throws CloneNotSupportedException{
	 try{
	     return (PresencePacket)super.clone();
	 }catch(CloneNotSupportedException ex){
	     throw ex;
	 }
     }
     
    public enum PresenceType {
	AVAILABLE, // available presence is the packet with no type attribute <presence></presence>
	UNAVAILABLE, 
	SUBSCRIBE, 
	UNSUBSCRIBE,
	SUBSCRIBED,
	UNSUBSCRIBED,
	ERROR,
	PROBE;

	@Override
	public String toString() {
	    return super.toString().toLowerCase();
	}
    }
}
